// https://leetcode.cn/problems/swap-nodes-in-pairs/

// 给你一个链表，两两交换其中相邻的节点，并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题（即，只能进行节点交换）。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
#include <vector>
#include <iostream>

using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
		if(!head || !head->next){
			return head;
		}
		
		ListNode * ret = head;
		
		ListNode * slow = head;
		ListNode * fast = head->next;
		
		
		while(fast){
			int tmp = slow->val;
			slow->val = fast->val;
			fast->val = tmp;
			if(fast->next){
				cout << 123 << endl;
				slow = fast->next;
				fast = fast->next;
				if(fast->next){
					fast = fast->next;
				}else{
					break;
				}
			}else{
				break;
			}
		}
		return ret;
    }
	
	ListNode * init(){
		vector<int> init = {4, 3, 2};
		int len = init.size();
		
		ListNode * head = new ListNode(init[0]);
		ListNode * index = head;
		for(int i = 1; i < len; i++){
			ListNode * tmp = new ListNode(init[i]);
			index->next = tmp;
			index = index->next;
		}
		return head;
	}
	
	void show(ListNode * head){
		while(head){
			cout << head->val << ",";
			head = head->next;
		}
		cout <<endl;
	}
};


int main(){
	Solution so;
	ListNode * head = so.init();
	so.show(head);
	ListNode * h1 = so.swapPairs(head);
	so.show(h1);
	return 0;
}